package com.hzcx.app.simplechat.ui.friend.adapter;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.ui.friend.bean.FriendRequestBean;
import com.hzcx.app.simplechat.ui.home.AddFriendActivity;
import com.hzcx.app.simplechat.ui.user.UserInfoActivity;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;

import java.util.ArrayList;
import java.util.List;

public class FriendRequestAdapter extends BaseQuickAdapter<FriendRequestBean, BaseViewHolder> {

    private OnAgreeFriendRequestListener listener;

    public FriendRequestAdapter(@Nullable List<FriendRequestBean> data) {
        super(R.layout.rv_item_friend_request_header, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, FriendRequestBean item) {

        TextView tvHeader = helper.getView(R.id.tv_header);
        RecyclerView rvContent = helper.getView(R.id.rv_content);

        List<FriendRequestBean.OneDataBean> list = new ArrayList<>();
        FriendRequestContentAdapter contentAdapter = new FriendRequestContentAdapter(list);
        rvContent.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        rvContent.setAdapter(contentAdapter);

        contentAdapter.setOnItemClickListener((adapter, view, position) -> {
            if(list.get(position).getState()==1){
                mContext.startActivity(new Intent(mContext, UserInfoActivity.class)
                        .putExtra(UserInfoActivity.INTENT_USER_ID, list.get(position).getMember_id()));
            }else {
                mContext.startActivity(new Intent(mContext, AddFriendActivity.class)
                        .putExtra(AddFriendActivity.INTENT_USER_ID, list.get(position).getMember_id()));
            }
        });

        contentAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            switch (view.getId()){
                case R.id.btn_send:
                    listener.agree(helper.getAdapterPosition(),position,list.get(position).getBuddy_id());
                    break;
            }
        });

        if (item == null || EmptyUtils.isEmpty(item.getOne()) || item.getOne_data() == null || item.getOne_data().size() <= 0) {
            tvHeader.setVisibility(View.GONE);
            rvContent.setVisibility(View.GONE);
        } else {
            tvHeader.setVisibility(View.VISIBLE);
            rvContent.setVisibility(View.VISIBLE);

            tvHeader.setText(item.getOne());
            list.addAll(item.getOne_data());
            contentAdapter.notifyDataSetChanged();
        }

    }

    public interface OnAgreeFriendRequestListener{
        void agree(int headerPos,int contentPos,int id);
    }

    public void setOnAgreeFriendRequestListener(OnAgreeFriendRequestListener listener){
        this.listener=listener;
    }
}
